import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
 
  {
    path: '/',
    redirect: '/index/home',
  },
 
  {
    path: '/index',
    name: 'index',
    component: () => import( '../views/IndexView.vue'),
    children:[
      {
        path: '/index/home',
        name: 'home',
        component: () => import( '../views/Index/Home.vue')
      },
      {
        path: '/index/boom',
        name: 'boom',
        component: () => import( '../views/Index/BoomView.vue'),
        children:[
          {
            path: '/Tuan1',
            name: 'Tuan1',
            component: () => import( '../views/Index/Tuan/Tuan1.vue')
          },
          {
            path: '/Tuan2',
            name: 'Tuan2',
            component: () => import( '../views/Index/Tuan/Tuan2.vue')
          },
        ]
      },
      {
        path: '/index/order',
        name: 'order',
        component: () => import( '../views/Index/Order.vue')
      },
      {
        path: '/index/mine',
        name: 'mine',
        component: () => import( '../views/Index/Mine.vue')
      },
     
    ]
  },
  {
    path: '/shop',
    name: 'shop',
    component: () => import( '../views/Index/Shop.vue')
  },
  {
    path: '/search',
    name: 'search',
    component: () => import( '../views/Index/SearchView.vue')
  },
  {
    path: '/login',
    name: 'login',
    component: () => import( '../views/Login.vue')
  },
  {
    path: '/register',
    name: 'register',
    component: () => import( '../views/Register.vue')
  },
  

]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to,from,next)=>{
  //from 当前路由对象
  //to 想要跳往的路由对象
  //next 路由控制函数
 
  
  var token = localStorage.getItem('token');
  if( to.path == '/login' ){ //想要跳往 登录页
    if( token ){ //已登录
      next('/index/home');
    }else{ //未登录
      next();
    }
  }else{ //想要跳往 其他页面
    if( token ){ //已登录
      next();
    }else{ //未登录
      next('/login');
    }
  }

  next();
})
export default router
